﻿using System;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;

public partial class Evaluate_InspectNewDatachment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320301"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["InspectID"] == null)
            {
                ActionTitle.Text = "添加工作督察项目";
            }
            else
            {
                rbType.Enabled = false;
                ActionTitle.Text = "编辑工作督察项目";
                BindData();
            }
        }
    }
    private void BindData()
    {
        InspectInfo inspect = Inspect.GetInspect(int.Parse(Request.QueryString["InspectID"]));
        InspectName.Text = inspect.InspectName;
        InspectDate.Text = inspect.InspectDate.ToShortDateString();
        TotalScore.Text = inspect.TotalScore.ToString("#");
        Description.Text = inspect.Description;
        rbType.SelectedValue = inspect.Flag.ToString();
    }
    private void Insert()
    {
        InspectInfo inspect = new InspectInfo();
        inspect.InspectName = InspectName.Text;
        inspect.InspectDate = DateTime.Parse(InspectDate.Text);
        inspect.TotalScore = decimal.Parse(TotalScore.Text);
        inspect.Description = Description.Text;
        inspect.DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 6) + "0";
        inspect.Flag = Convert.ToInt32(rbType.SelectedValue);
        int InspectID = Inspect.Insert(inspect);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "添加工作督察项目";
        log.ItemID = InspectID.ToString();
        log.ItemName = "Inspects";
        log.ItemFullUrl = "InspectNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        InspectInfo inspect = Inspect.GetInspect(int.Parse(Request.QueryString["InspectID"]));
        inspect.InspectName = InspectName.Text;
        inspect.InspectDate = DateTime.Parse(InspectDate.Text);
        inspect.TotalScore = decimal.Parse(TotalScore.Text);
        inspect.Description = Description.Text;
        inspect.Flag = Convert.ToInt32(rbType.SelectedValue);
        Inspect.Update(inspect);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "更新工作督察项目";
        log.ItemID = Request.QueryString["InspectID"];
        log.ItemName = "Inspects";
        log.ItemFullUrl = "InspectNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["InspectID"] == null)
                Insert();
            else
                Update();
        }
        catch (Exception ex)
        {
            Prompt.Text = "错误：" + ex.Message;
        }
    }
}
